﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AAPCVM
{
    public partial class Login : Form
    {
        private ErrorProvider errProvider = new ErrorProvider();

        public bool IsGenerateReportDialog { get; set; }

        char[] alphabet = new char[] { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        public int BallotID { get; set; }

        public Login()
        {
            InitializeComponent();
            BallotID = 0;
            IsGenerateReportDialog = false;
        }

        private void txtBallot_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar))
                e.Handled = true;
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            errProvider.Clear();

            if (txtBallot.Text == string.Empty)
            {
                errProvider.SetError(txtBallot, "Please enter a valid Ballot Id");
                return;
            }

            if (txtPassword.Text == string.Empty)
            {
                errProvider.SetError(txtPassword, "Please enter a valid password");
                return;
            }

            BallotID = int.Parse(txtBallot.Text);
            //string password = "arvind";
            if (CheckCredentials(BallotID, txtPassword.Text))
            {
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                return;
            }
        }

        private bool CheckCredentials(int ballotId, string p)
        {
            string key = "Swaraj";
            string sExpectedPwd = DecryptPassword(ballotId, key);
            if (p.Equals(sExpectedPwd))
            {
                return true;
            }
            return false;
        }

        private string DecryptPassword(int ballotid, string key)
        {
            string pwd = string.Empty;
            if (IsGenerateReportDialog)
            {
                IsGenerateReportDialog = false;
                pwd = key + ballotid + "AKparivartan";
            }
            else
            {
                pwd = key + ballotid + "4Mum";
            }

            return pwd;
        }

        private string Encrypt(string password, int key)
        {
            //char[] str = password.ToCharArray();
            //int index = 0;
            //for (int i = 0; i < str.Length; i++)
            //{
            //    for (int j = 0; j < alphabet.Length; j++)
            //        if (str[i] == alphabet[j])
            //        {
            //            index = j;
            //            break;
            //        }

            //}

            return string.Empty;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void Login_Load(object sender, EventArgs e)
        {
            if (BallotID != 0)
            {
                txtBallot.Text = BallotID.ToString();
                txtBallot.ReadOnly = true;
            }
        }
    }
}
